# 数据库链接
from xjlibrary.database_moudel.simple.mysqlclient import MySqlDbConnect, SelctSqlFromDB, ExeSqlList
from xjlibrary.our_file_dir.base_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)

listsql = []


def MajorDbConnect():
    return MySqlDbConnect(curPath, 'db.ini')


def selectFromDB():
    sql = "select `starttime`,`endtime`,`allpage` from timeeapo where stat=0"
    rows = SelctSqlFromDB(sql, MajorDbConnect())
    return rows


def main():
    global listsql
    rows = selectFromDB()
    if rows:
        for row in rows:
            starttime = row[0]
            endtime = row[1]
            allpage = row[2]
            for page in range(1, int(allpage) + 1):
                sql = "insert ignore into eapopage (starttime,endtime,page) values('{}','{}','{}')".format(starttime
                                                                                                           , endtime
                                                                                                           , page)
                listsql.append(sql)
            sql = "update timeeapo set stat=1 where starttime='{}' and endtime='{}'".format(starttime, endtime)
            listsql.append(sql)
            ExeSqlList(listsql, MajorDbConnect())
            listsql.clear()
    else:
        print("数据库状态为0的数据为空")


if __name__ == "__main__":
    main()
